/**   
 * All rights Reserved, Designed By MiGu  
 * Copyright:    Copyright(C) 2016-2020  
 * Company       MiGu  Co., Ltd.
*/
package com.migu.spms.webapi;


import java.io.IOException;
import java.io.StringWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.migu.spms.constant.Constant;
import com.migu.spms.dao.entity.FunctionRequireCriteria;
import com.migu.spms.service.data.PaginationResult;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IRequirementService;
import com.migu.spms.webapi.data.CommentResponse;
import com.migu.spms.webapi.data.CorrelaRequireResponse;
import com.migu.spms.webapi.data.RequirementDetailInfo;
import com.migu.spms.webapi.data.RequirementDetailsResponse;
import com.migu.spms.webapi.data.RequirementInfo;

/**
 * RequirementController类 项目名称: SPMS功能需求 包名称: com.migu.spms.webapi 类名称:
 * RequirementController.java 类描述: 功能需求 创建人: xs 创建时间: 2017年6月13日 上午9:27:52
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "/requirement", produces = "application/json;charset=UTF-8")
public class RequirementController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(RequirementController.class);
	
	@Resource(name="requirementServiceImpl")
	private IRequirementService requirementService;
	 
	/** 功能需求详情
	 * 
	 * @param req
	 * @param resp
	 * @param sessionId
	 * @return
	 * @exception @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/requireDetails")
	public String details(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		RequirementDetailsResponse requirementDetailsResponse = requirementService.requireDeatils(functionRequireCriteria,user);
		String result=generateResponse(requirementDetailsResponse, 200);
		return result;
	}
	
	/** 功能需求详情,评论回显接口
	 *
	 */
	@ResponseBody
	@RequestMapping(value = "/queryCommentList")
	public String queryCommentList(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		CommentResponse commentResponse = requirementService.queryCommentList(functionRequireCriteria,user);
		return generateResponse(commentResponse, 200);
	}
	
	
	/** 功能需求详情关联需求分页
	 * 
	 * @param req
	 * @param resp
	 * @param sessionId
	 * @return
	 * @exception @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/requireDetailsCorrelation")
	public String requireDetailsCorrelation(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		PaginationResult<RequirementDetailInfo> paginationResult = requirementService.requireDetailsCorrelation(functionRequireCriteria, user);
		return generateResponse(paginationResult, 200);
	}
	
	/**
	 * 查询关联需求是否存在和名字
	 */
	@ResponseBody
	@RequestMapping(value ="/querycorrelaRequireName")
	public String querycorrelaRequireName(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		CorrelaRequireResponse correlaReruireResponse = requirementService.queryCorrelaRequirementName(functionRequireCriteria);
		return generateResponse(correlaReruireResponse, code);
	}
	
	/**
	 * 关联需求
	 */
	@ResponseBody
	@RequestMapping(value ="/correlaRequire")
	public String correlaRequire(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		String msg = requirementService.correlaRequire(functionRequireCriteria);
		return generateResponse(200);
	}
	
	/**
	 * 返回所有需求
	 */
	@ResponseBody
	@RequestMapping(value ="/allRequireByPage")
	public String allRequireByPage(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		String myCurrent = functionRequireCriteria.getMyCurrent();
		if(myCurrent != null && myCurrent != ""){
			if(myCurrent.equals(Constant.MyCurrent.ALL_REQUIRE)){
				PaginationResult<RequirementInfo> paginationResult = requirementService
						.allRequireByPage(functionRequireCriteria,user);
				return generateResponse(paginationResult, 200);
			}else{
				PaginationResult<RequirementInfo> paginationResult = requirementService
						.myCurrent(functionRequireCriteria,user);
				return generateResponse(paginationResult, 200);
			}
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value ="/homePageWaitingHandle")
	public String queryHomePageWaitingHandle(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		int code = 200;
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
		PaginationResult<RequirementInfo> paginationResult = requirementService.queryHomePageWaitingHandle(functionRequireCriteria,user);
		return generateResponse(paginationResult, 200);
	}
	
//	/**
//	 * 功能需求列表
//	 * 
//	 * @param req
//	 * @param resp
//	 * @param sessionId
//	 * @return
//	 * @exception @throws
//	 */
//	@ResponseBody
//	@RequestMapping(value = "/listByBasePage")
//	public String list(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
//		int code = 200;
//		User user = getUserBySession(req, sessionId);
//		if (user == null) {
//			return generateResponse(401);
//		}
////		User user = new User();
////		user.setUserId(3);
//		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
//		PaginationResult<RequirementInfo> paginationResult = requirementService
//				.queryRequirementList(functionRequireCriteria,user);
//		return generateResponse(paginationResult, 200);
//	}
//	
//	/** 功能需求列表子需求
//	 * 
//	 * @param req
//	 * @param resp
//	 * @param sessionId
//	 * @return
//	 * @exception @throws
//	 */
//	@ResponseBody
//	@RequestMapping(value = "/requireSubQuery")
//	public String requireSubQuery(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
//		int code = 200;
//		User user = getUserBySession(req, sessionId);
//		if (user == null) {
//			return generateResponse(401);
//		}
////		User user = new User();
////		user.setUserId(3);
//		FunctionRequireCriteria functionRequireCriteria = ParseMsg(req, FunctionRequireCriteria.class);
//		List<RequirementInfo> requireSubList = requirementService
//				.requireSubQuery(functionRequireCriteria,user);
//		
//		RequirementListResponse requirementListResponse = new RequirementListResponse();
//		requirementListResponse.setRequirementInfoSubList(requireSubList);
//		return generateResponse(requirementListResponse, 200);
//	} 
}
